$(function(){
    loginName=baseFunction.getUrlParameterValueByKey("loginName");
    id=baseFunction.getUrlParameterValueByKey("id"); //通道ID
    bankListData=""; //信用卡列表的数据
    currentBankCard=""; //当前选中用于取现的信用卡卡号
    currentBankCardName=""; //当前选中用于取现的信用卡昵称
    settlementCard=""; //结算卡的账号
    settlementCardName=""; //结算卡的账号昵称
    bizType=""; //渠道编号
    maxAmount="";//提现最大金额
    minAmount=""; //提现最小金额
    rate=""; //费率 加上了额外费率了的
    cashMoney=""; //提现的手续费
    getBaseData();
    getBankList(); //判断是否绑定信用卡
    getBankInfo(); //结算卡信息


    //点击事件
    // 没有添加信用卡时  点击添加信用卡
    $(".bankCard .creditCard .right").on("click",".noBank",function(){
        baseFunction.verifyingRealName(function(){
            window.location.href="my_addBankCard.html?loginName="+loginName+"";
        })
    })
    $(".template .addBank").on("click",function(){
        baseFunction.verifyingRealName(function(){
            window.location.href="my_addBankCard.html?loginName="+loginName+"";
        })
    })
    // 添加了信用卡时  店家查看信用卡详情
    $(".bankCard .creditCard .right").on("click",".bank .details",function(){
        var bankId=$(this).attr("data-id");
        window.location.href="my_modifyCreditCard.html?loginName="+loginName+"&id="+bankId+"";
       
    })
    // 更换提现信用卡蒙版显示
    $(".bankCard .creditCard .right").on("click",".replace",function(){
        $(".template").show(0);
    })
    //蒙版隐藏
    $(".template .cancel").on("click",function(){
        $(".template").hide(0);
    })
    // 切换提现信用卡
    $(".template .list").on("click",".tabBank",function(){
        var tip=$(this).attr("data-id");
        var  html="";
        bankListData.forEach(function(key){
            if(key.id==tip){
                currentBankCard=key.cardno;
                currentBankCardName=key.bankName;
                html="<div class='bank'>"
                    +"<div class='bankL'>"
                    +"<img src='"+key.bg+"'>"
                    +"<text>"+key.bankName+"  ("+key.endNO+")</text>"
                    +"</div>"
                    +"<div class='bankR'>"
                    +"<div class='details' data-id='"+key.id+"' >卡片信息</div>"
                    +"<img src='../static/img/button_change@2x.png' class='replace'>"
                    +"</div>"  
                    +"</div>"
            }
        })
        $(".bankCard .creditCard .right").html(html);
        $(".template").hide(0);
    })

    // 提现
    $("#submit").on("click",function(){
        submit();
    })

})
/**
 * 获取基本数据
 */
function getBaseData(){
    $(".loding").show();
    var data={
        userName: baseFunction.encryption(loginName),
        queryType:"6021",
        arg1: baseFunction.encryption("4"),
    };
    baseFunction.netRequest("param",data,function(res){
        
        var dataList=JSON.parse(JSON.parse(res.data).result);
        var addRate="";
        dataList.forEach(function(key){
            if(id==key.id){
                console.log(key);
                addRate=key.addRate; //费率
                bizType=key.channelCode; //渠道编号
                maxAmount=key.maxAmount; //提现最大金额
                minAmount=key.minAmount; //提现最小金额
            }
        });
        $(".loding").hide();
        getCashRate(addRate);
    })
}

/**
 * 获取取现费率
 */
function getCashRate(addRate){
    var data={
      userName: baseFunction.encryption(loginName),
      queryType: "4311",
    };
    baseFunction.netRequest("param",data,function(res){
      
      var data = JSON.parse(JSON.parse(res.data).result);
      rate = Number(addRate)+Number(data.quick_t0) ; //费率
      cashMoney=data.quick_wd_fee //提现费
      var text = cashMoney == 0 ? "费率" + (rate * 100).toFixed(2) + "%，立即到账，可往还款通道‘充值’并‘体现’" : "费率" + (rate * 100).toFixed(2) + "%+" + cashMoney+"元，立即到账，可往还款通道‘充值’并‘体现’";
      $(".main .kehuTip").html(text);
    })
}
/**
 * 获取结算卡的信息
 */
function getBankInfo () {
    var userData=baseFunction.getUserInfo();
    var data = JSON.parse(JSON.parse(userData).merch);
    settlementCard=data.accountno?data.accountno:"";
    settlementCardName=data.bankName?data.bankName:"";
    if (!data.bankTypeName){ //没有绑定结算卡
        return;
    }
    var imgString; //图片路径
    for (var j = 0; j < bankCard.length; j++) {
        if (data.bankTypeName.indexOf(bankCard[j].bankName) != -1) {
            imgString = "../static/img/" + bankCard[j].bigLogo + ".png";
        }
    }
    var bankInfo={
        cardNo: (data.accountno).slice(-4), //卡号
        bankTypeName: data.bankTypeName, //银行名字
        imgs: imgString
    }
    var html="";
    if(bankInfo.cardNo){
        html+="<div class='binded'>"
            +"<img src='"+bankInfo.imgs+"'>"
            +"<text>"+bankInfo.bankTypeName+" ("+bankInfo.cardNo+")</text>"
            +"</div>";
    }else{
        html+="<div class='unionPayCards'>"
            +"<img src='../static/img/quick_unionpay.png'>"
            +"<text>银联卡</text>"
            +"</div>"
    }
    $(".bankCard .debitCard .right").html(html);
}

/**
 * 获取银行卡列表(判断是否绑定信用卡)
 */
function getBankList () {
    var data = {
        userName: baseFunction.encryption(loginName)
    };
    baseFunction.netRequest("quickCardBind", data, function (res) {
         //解密
        if (res.code == 200) {
            var data = JSON.parse(JSON.parse(res.data).cards);
            currentBankCard=data[0]?data[0].cardno:"";
            currentBankCardName=data[0]?data[0].bankName:"";
            for (var i = 0; i < data.length; i++) {
                data[i].endNO = (data[i].cardno).slice(-4);
                for (var j = 0; j < bankCard.length; j++) {
                    if ((data[i].bankName).indexOf(bankCard[j].bankName)!=-1) {
                        data[i].bg = "../static/img/" +bankCard[j].bigLogo + ".png"
                    }
                }
            }
            bankListData=data;
            var html="";
            if(data.length==0){
                html="<div class='noBank'>"
                    +"<img src='../static/img/add_creditcard_icon.png'>"
                    +"<text>添加信用卡</text>"
                    +"</div>";
                
            }else{
                html="<div class='bank'>"
                    +"<div class='bankL'>"
                    +"<img src='"+data[0].bg+"'>"
                    +"<text>"+data[0].bankName+"  ("+data[0].endNO+")</text>"
                    +"</div>"
                    +"<div class='bankR'>"
                    +"<div class='details' data-id='"+data[0].id+"' >卡片信息</div>"
                    +"<img src='../static/img/button_change@2x.png' class='replace'>"
                    +"</div>"  
                    +"</div>"
            }
            $(".bankCard .creditCard .right").html(html);
            // bom 更换行用卡提现的蒙版
            var html2="";
            data.forEach(function(key){
                html2+="<div class='tabBank' data-id='"+key.id+"'>"+key.bankName+"（<text>"+key.endNO+"</text>）</div>"
            })
            $(".template .list").html(html2);
        } 
    })
}


/**
 * 确认取现
 */
function submit(){
    var money=$("#money").val();
    // 已对本地业务判断
    if (bankListData.length == 0){ //没有绑定信用卡
        $(".prompt").html("您还未绑定取现信用卡，请先去绑定！").show(0).delay(1000).hide(0);
        return;
    } else if (!settlementCard){ //没有绑定结算卡
        $(".prompt").html("您还未绑定到账结算卡，请先去绑定！").show(0).delay(1000).hide(0);
        return;
    } else if (minAmount > money ){ //额度太小
        $(".prompt").html("您取现的金额过小，最小取现额度是" + minAmount+"元！").show(0).delay(1000).hide(0);
        return;
    } else if (maxAmount < money){ //额度太大
        $(".prompt").html("您取现的金额过大，最大取现额度是" + maxAmount+"元！").show(0).delay(1000).hide(0);
        return;
    }
    $(".loading").show(0);
    var data={
      userName: baseFunction.encryption(loginName),
      cardno: baseFunction.encryption(currentBankCard),
      amount: baseFunction.encryption(money),
      settleType: 1,
      bizType: baseFunction.encryption(bizType),
    };
    baseFunction.netRequest("quickOrder",data,function(res){
        $(".loading").hide(0);
      
      console.log(res);
      $(".prompt").html(res.message).show(0);
      if (res.code == 200 && JSON.parse(res.data).status==1 ){ //提现成功
        // 准备数据
        var orderno = JSON.parse(res.data).orderno;  //订单号
        var takeCashAmount = money;//取现金额
        var arrivalAmount = (Number(money) - Number(rate) * Number(money) - Number(cashMoney)).toFixed(2);//到账金额
        var creditCard =currentBankCardName + "  (" + currentBankCard.slice(-4)+")";//取现信用卡
        var savingsCard = settlementCardName + "  (" + settlementCard + ")";//到账储蓄卡

        if (JSON.parse(res.data).payMode =="H5"){ //如果是H5支付的话  直接跳转链接
           var url=JSON.parse(res.data).barCode;
           setTimeout(function(){
               window.location.href=url;
           },1000);
           return;
        }else if (JSON.parse(res.data).flagMsg == "1"){ //跳转到快捷确认页面 然后在跳转去成功页面
           setTimeout(function(){
               window.location.href='index_verification.html?loginName=' + loginName + '&orderno=' + orderno + '&takeCashAmount=' + takeCashAmount + '&arrivalAmount=' + arrivalAmount + '&creditCard=' + creditCard + '&savingsCard=' + savingsCard +'';
           },1000);
           return;
        } else if (JSON.parse(res.data).transType == "1") { //直接跳转去成功页面
          setTimeout(function () {
              window.location.href='index_takeCashSuccess.html?orderno=' +orderno + '&takeCashAmount=' + takeCashAmount + '&arrivalAmount=' + arrivalAmount + '&creditCard=' + creditCard + '&savingsCard=' + savingsCard +'';
          }, 1000);
          return;
        }
      }
    })
}